<?php

namespace common\modules\wechat\models\backend;

use common\widgets\ActiveForm;
use common\widgets\Form;
use common\widgets\Select2;
use Yii;

class AccountForm extends Account
{

    const SCENARIO_CREATE = 'admin-create';
    const SCENARIO_UPDATE = 'admin-update';

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        return [
            'admin-create' => array_keys($this->attributeLabels()),
            'admin-update' => array_keys($this->attributeLabels()),
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        // 新创建只需两个填写两个字段
        if ($this->isNewRecord || $this->getScenario() == self::SCENARIO_CREATE)
        {
            return [
                [['name', 'description'], 'required'],
                [['status'], 'defaultStatus'],
            ];
        }
        return parent::rules();
    }

    /**
     * 新创建的公众号默认是未激活状态
     *
     * @param $attribute
     * @param $params
     */
    public function defaultStatus($attribute, $params)
    {
        if ($this->isNewRecord)
        {
            $this->status = self::STATUS_INACTIVE;
        }
    }

    /**
     * 返回当前表单的名称
     *
     * @return string
     */
    public function classLabelName()
    {
        return Yii::t('backend/wechat', 'Wechat Account Form');
    }

    /**
     * 列表定义
     *
     * @param ActiveForm|Form $form
     * @param static  $model
     * @param mixed  $extra
     * @return array
     * @throws \yii\web\ServerErrorHttpException
     */
    public function formRows(&$form, $model, $extra)
    {
        return [
            [
                'columns'    => 3,
                'attributes' => [
                    'id' => [
                        'type'    => Form::INPUT_TEXT,
                        'options' => ['disabled' => true],
                    ],
                    'original' => [
                        'type' => Form::INPUT_TEXT,
                    ],
                    'account' => [
                        'type' => Form::INPUT_TEXT,
                    ],

                    'type' => [
                        'type' => Form::INPUT_WIDGET,
                        'widgetClass' => Select2::className(),
                        'options' => [
                            'data' => self::$types,
                        ],
                    ],
                    'name' => [
                        'type' => Form::INPUT_TEXT,
                    ],
                    'username' => [
                        'type' => Form::INPUT_TEXT,
                    ],
                    /*'password' => [
                        'type' => Form::INPUT_TEXT,
                    ],*/

                    'status' => [
                        'type' => Form::INPUT_WIDGET,
                        'widgetClass' => Select2::className(),
                        'options' => [
                            'data' => self::$statuses,
                        ],
                    ],
                    'avatar' => [
                        'type' => Form::INPUT_TEXT,
                    ],
                    'qrcode' => [
                        'type' => Form::INPUT_TEXT,
                    ],

                    'key' => [
                        'type' => Form::INPUT_TEXT,
                    ],
                    'secret' => [
                        'type' => Form::INPUT_TEXT,
                    ],
                    'encoding_aes_key' => [
                        'type' => Form::INPUT_TEXT,
                    ],
                ],
            ],
            [
                'columns'    => 1,
                'attributes' => [
                    'description' => [
                        'type' => Form::INPUT_TEXTAREA,
                    ],
                    'address' => [
                        'type' => Form::INPUT_TEXT,
                    ],
                ],
            ],
            [
                'columns'    => 2,
                'attributes' => [

                    'token' => [
                        'type' => Form::INPUT_TEXT,
                    ],
                    'access_token' => [
                        'type' => Form::INPUT_TEXT,
                    ],
                ],
            ],
            [
                'columns'    => 4,
                'attributes' => [
                    'created_at' => [
                        'type'        => Form::INPUT_STATIC,
//                        'staticValue' => function ($model, $key, $index)
//                        {
//                            /**
//                             * @param $this $model
//                             */
//                            return $model->created_at
//                                ? date('Y-m-d H:i:s', $model->created_at)
//                                : '--';
//                        },
                    ],

                    'updated_at' => [
                        'type'        => Form::INPUT_STATIC,
//                        'staticValue' => function ($model, $key, $index)
//                        {
//                            /**
//                             * @param $this $model
//                             */
//                            return $model->updated_at
//                                ? date('Y-m-d H:i:s', $model->updated_at)
//                                : '--';
//                        },
                    ],

                    'created_by' => [
                        'type' => Form::INPUT_STATIC,
                    ],
                    'updated_by' => [
                        'type' => Form::INPUT_STATIC,
                    ],
                ],
            ],
        ];
    }
}